
<?php

$con=mysql_connect("localhost","root","123");

if(!$con)
{
die('Could bot connect: '.mysql_error());
}
else{
echo"connect successfully";
}

function createFlowersTable(){
   global $con;
 $db_select = mysql_select_db("cs2102_db", $con);
    $sql="CREATE TABLE Flowers(
      fname VARCHAR(50), 
	  category VARCHAR(20) NOT NULL,
      occasion VARCHAR(30) NOT NULL, 
      quantity INTEGER NOT NULL, 
      color VARCHAR(10) NOT NULL, 
	  picture VARCHAR(200) NOT NULL,
      size CHAR(1) CHECK(size IN('L', 'M', 'S')), 
      price REAL NOT NULL,
      PRIMARY KEY(fname,size)
	 )";
 if(mysql_query($sql,$con)){
   echo"Flowers table created";
   }
 else{
   echo "FlowersTable is not created";
	die('Could not create: ' . mysql_error());
	}
}
function insertTuple(){
	global $con;
	$sql="INSERT INTO Flowers(fname, category, occasion, quantity, color, size, price,picture)
		VALUES('$_POST[fname]','$_POST[category]','$_POST[occasion]','$_POST[quantity]','$_POST[color]','$_POST[size]','$_POST[price]','$_POST[address]')";
	if (!mysql_query($sql,$con)){
		die('Error: ' . mysql_error());
		return false;
	}else{
		echo "1 record added";
		return true;
	}
}
function IsExistTable($table){
	global $con;
	$sql = "show tables like '".$table."'";
	$res = mysql_query($sql,$con);
	return ($res);
}

$db_select = mysql_select_db("cs2102_db", $con);

if (!$db_select){
	die('Cannot use database cs2102_db'. mysql_error());
}

if (!isExistTable("Flowers")){
createFlowersTable();
}
insertTuple();

mysql_close($con);
header("location: Admin_flower.php");
?>


